package com.qilu.pe.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.makeramen.roundedimageview.RoundedImageView;
import com.qilu.pe.R;
import com.qilu.pe.base.BaseResult2;
import com.qilu.pe.base.Global;
import com.qilu.pe.dao.bean.EventModel;
import com.qilu.pe.dao.bean.Patient;
import com.qilu.pe.dao.http.APIRetrofit;
import com.qilu.pe.support.util.GlideUtil;
import com.ruitu.arad.Arad;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.base.BaseFragment;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

// 患者选择--我的机构
public class ChoosePatient1Fragment extends BaseFragment {
    private RecyclerView rcv_list;

    private ChoosePatientListAdapter adapter;
    private List<Patient> patientList = new ArrayList<>();

    public ChoosePatient1Fragment() {

    }

    public static ChoosePatient1Fragment newInstance(String status) {
        ChoosePatient1Fragment fragment = new ChoosePatient1Fragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        adapter = new ChoosePatientListAdapter(getActivity());

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_choose_patient1, container, false);
        rcv_list = v.findViewById(R.id.rcv_list);

        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        rcv_list.setLayoutManager(manager);
        rcv_list.setAdapter(adapter);

        setListeners();

        return v;
    }

    @Override
    public void onResume() {
        super.onResume();
        hideProgress();
        reqPatientListAgent();
        showProgress();
    }

    private void setListeners() {
        adapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                Arad.bus.post(new EventModel(20092351, patientList.get(position)));
                getActivity().finish();
            }
        });
    }

    private Disposable reqPatientListAgent() {
        return APIRetrofit.getDefault().reqPatientListAgent(Global.HEADER, "time")
                .compose(RxUtil.<BaseResult2<List<Patient>>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<List<Patient>>>() {
                    @Override
                    public void accept(BaseResult2<List<Patient>> listBaseResult2) throws Exception {
                        hideProgress();
                        if (listBaseResult2.isSuccess()) {

                        } else {

                        }
                        patientList = listBaseResult2.getData();
                        adapter.setDataList(patientList);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                    }
                });
    }

    private class ChoosePatientListAdapter extends ListBaseAdapter<Patient> {

        public ChoosePatientListAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_choose_patient_list_1;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            TextView tv_name = holder.getView(R.id.tv_name);
            TextView tv_sex = holder.getView(R.id.tv_sex);
            TextView tv_phone = holder.getView(R.id.tv_phone);
            ImageView iv_sex = holder.getView(R.id.iv_sex);
            RoundedImageView iv_header = holder.getView(R.id.iv_header);

            Patient patient = mDataList.get(position);
            tv_name.setText(patient.getName());
            tv_sex.setText(TextUtils.equals("1", patient.getSex()) ? "女" : "男");
            iv_sex.setImageResource(TextUtils.equals("1", patient.getSex()) ?
                    R.mipmap.ic_patient_sex_female : R.mipmap.ic_patient_sex_male);// ic_patient_sex_female ic_patient_sex_male
            tv_phone.setText("电话：" + patient.getPhone());
            GlideUtil.loadImg(patient.getPicture(), iv_header);
        }
    }
}